ostree: Add a "remote refs" command
authorMatthew Barnes <mbarnes@redhat.com>
Sun, 7 Jun 2015 19:20:14 +0000 (15:20 -0400)
committerGiuseppe Scrivano <gscrivan@redhat.com>
Fri, 26 Jun 2015 09:02:24 +0000 (11:02 +0200)
Works like "ostree refs" but fetches refs from a remote repo.

This depends on the remote repo having a summary file, but any repo
being served over HTTP *ought* to have one.

Makefile-ostree.am
doc/ostree-remote.xml
src/ostree/ot-builtin-remote.c
src/ostree/ot-remote-builtin-refs.c [new file with mode: 0644]
src/ostree/ot-remote-builtins.h
tests/test-pull-metalink.sh

index 9ae9870a085aac6d7fbb3a8cef92869106b0bd86..8fd426f764d955f59953eb38e10126664043c13f 100644 (file)
@@ -80,6 +80,7 @@ ostree_SOURCES += \
        src/ostree/ot-remote-builtin-gpg-import.c \
        src/ostree/ot-remote-builtin-list.c \
        src/ostree/ot-remote-builtin-show-url.c \
+        src/ostree/ot-remote-builtin-refs.c \
        $(NULL)
 
 ostree_bin_shared_cflags = $(AM_CFLAGS) -I$(srcdir)/src/libotutil -I$(srcdir)/src/libostree -I$(srcdir)/src/ostree \
index 7b643f296a4ce1e240d386aa6c72ab8b3f459622..ae9e078ff6743017b3d998fdfeb94dff3581a91f 100644 (file)
@@ -63,6 +63,9 @@ Boston, MA 02111-1307, USA.
             <cmdsynopsis>
                 <command>ostree remote gpg-import</command> <arg choice="opt" rep="repeat">OPTIONS</arg> <arg choice="req">NAME</arg> <arg choice="opt" rep="repeat">KEY-ID</arg>
             </cmdsynopsis>
+            <cmdsynopsis>
+                <command>ostree remote refs</command> <arg choice="req">NAME</arg>
+            </cmdsynopsis>
     </refsynopsisdiv>
 
     <refsect1>
index c112063cf44c3b3faad06937220f3def3e179a03..4ea8d22e69b38e2ac473df3ef59d7451acd47e40 100644 (file)
@@ -37,6 +37,7 @@ static OstreeRemoteCommand remote_subcommands[] = {
   { "show-url", ot_remote_builtin_show_url },
   { "list", ot_remote_builtin_list },
   { "gpg-import", ot_remote_builtin_gpg_import },
+  { "refs", ot_remote_builtin_refs },
   { NULL, NULL }
 };
 
diff --git a/src/ostree/ot-remote-builtin-refs.c b/src/ostree/ot-remote-builtin-refs.c
new file mode 100644 (file)
index 0000000..c3dbbf2
--- /dev/null
@@ -0,0 +1,96 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2015 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include "otutil.h"
+
+#include "ot-main.h"
+#include "ot-remote-builtins.h"
+
+static GOptionEntry option_entries[] = {
+};
+
+gboolean
+ot_remote_builtin_refs (int argc, char **argv, GCancellable *cancellable, GError **error)
+{
+  GOptionContext *context;
+  glnx_unref_object OstreeRepo *repo = NULL;
+  const char *remote_name;
+  g_autoptr(GBytes) summary_bytes = NULL;
+  gboolean ret = FALSE;
+
+  context = g_option_context_new ("NAME - List remote refs");
+
+  if (!ostree_option_context_parse (context, option_entries, &argc, &argv,
+                                    OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error))
+    goto out;
+
+  if (argc < 2)
+    {
+      ot_util_usage_error (context, "NAME must be specified", error);
+      goto out;
+    }
+
+  remote_name = argv[1];
+
+  if (!ostree_repo_remote_fetch_summary (repo, remote_name,
+                                         &summary_bytes, NULL,
+                                         cancellable, error))
+    goto out;
+
+  if (summary_bytes == NULL)
+    {
+      g_print ("Remote refs not available; server has no summary file\n");
+    }
+  else
+    {
+      g_autoptr(GVariant) summary = NULL;
+      g_autoptr(GVariant) ref_map = NULL;
+      GVariantIter iter;
+      GVariant *child;
+
+      summary = g_variant_new_from_bytes (OSTREE_SUMMARY_GVARIANT_FORMAT,
+                                          summary_bytes, FALSE);
+
+      ref_map = g_variant_get_child_value (summary, 0);
+
+      /* Ref map should already be sorted by ref name. */
+      g_variant_iter_init (&iter, ref_map);
+      while ((child = g_variant_iter_next_value (&iter)) != NULL)
+        {
+          const char *ref_name = NULL;
+
+          g_variant_get_child (child, 0, "&s", &ref_name);
+
+          if (ref_name != NULL)
+            g_print ("%s\n", ref_name);
+
+          g_variant_unref (child);
+        }
+    }
+
+  ret = TRUE;
+
+out:
+  g_option_context_free (context);
+
+  return ret;
+}
index 286ac9a1552888a02e8665b69a7ac03bdaa37842..b7974a4c3ab37519f32bff62e3896e50e99840e1 100644 (file)
@@ -29,5 +29,6 @@ gboolean ot_remote_builtin_delete (int argc, char **argv, GCancellable *cancella
 gboolean ot_remote_builtin_gpg_import (int argc, char **argv, GCancellable *cancellable, GError **error);
 gboolean ot_remote_builtin_list (int argc, char **argv, GCancellable *cancellable, GError **error);
 gboolean ot_remote_builtin_show_url (int argc, char **argv, GCancellable *cancellable, GError **error);
+gboolean ot_remote_builtin_refs (int argc, char **argv, GCancellable *cancellable, GError **error);
 
 G_END_DECLS
index cd9631aefd7d6cb4db99959be065eaaf71d6cce8..085a72aef8d5bb2ec414e858670c25704c3ce695 100755 (executable)
@@ -74,6 +74,11 @@ ${CMD_PREFIX} ostree --repo=repo rev-parse origin:main
 ${CMD_PREFIX} ostree --repo=repo fsck
 echo "ok pull via metalink"
 
+# Test fetching the summary through ostree_repo_remote_fetch_summary()
+${CMD_PREFIX} ostree --repo=repo remote refs origin > origin_refs
+assert_file_has_content origin_refs "main"
+echo "ok remote refs via metalink"
+
 cp metalink-data/metalink.xml{,.orig}
 cp ostree-srv/gnomerepo/summary{,.orig}